<template>
	<view class="u-p-30">
		<view class="combox">
			<view class="u-font-18 f-w">
				请上传身份证正反面
			</view>
			<view class="u-flex u-flex-column u-flex-center u-m-t-30">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="1" width="300" height="180" :previewFullImage="false" :capture="['album','camera']">
					<image src="@/static/images/idfont.png" mode="widthFix"
						style="width: 300px;height: 180px;text-align: center;"></image>
				</u-upload>
			</view>
			<view class="u-flex u-flex-column u-flex-center u-m-t-30">
				<u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" name="2" multiple
					:maxCount="1" width="300" height="180" :previewFullImage="false" :capture="['album','camera']">
					<image src="@/static/images/idback.png" mode="widthFix"
						style="width: 300px;height: 180px;text-align: center;"></image>
				</u-upload>
			</view>
			<view class="u-font-18 f-w">
				证明照片
			</view> 
			<view class="u-flex u-flex-column u-flex-center u-m-t-30">
				<u-upload :fileList="fileList3" @afterRead="afterRead" @delete="deletePic" name="3" multiple
					:maxCount="1" width="300" height="180" :previewFullImage="false" :capture="['camera']">
					<image src="@/static/images/idd.png" mode="widthFix"
						style="width: 300px;height: 180px;text-align: center;"></image>
				</u-upload>
			</view>
			<u--form labelPosition="left" :model="userInfo" :rules="rules" ref="uForm">
				<u-form-item label="姓名:" label-width='70' prop="name" borderBottom ref="item1">
					<u--input v-model="userInfo.name" border="none" placeholder='请输入姓名'></u--input>
				</u-form-item>
				<u-form-item label="身份证号:" label-width='70' prop="identity" borderBottom ref="item1">
					<u--input v-model="userInfo.identity" border="none" placeholder='请输入身份证'></u--input>
				</u-form-item>
			</u--form>
		</view>
		<view class="u-p-30">
			<u-button @click="submit" type="primary" shape="circle">提交</u-button>
		</view>
	</view>
</template>
<script>
	import IDValidator from '../../../config/IDValidator.js'
	import url from '@/config/url.js';
	export default {
		data() {
			return {
				fileList1: [],
				fileList2: [],
				fileList3: [],
				baseUrl: url.baseUrl,
				userInfo: {
					identity: '',
					idFront: '',
					idBack: '',
					name: '',
				},
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					'identity': {
						type: 'string',
						required: true,
						message: '请填写身份证号',
						trigger: ['blur', 'change']
					},
				},
			}
		},

		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			showImage(pic) {
				uni.previewImage({
					urls: [pic]
				})
			},
			submit() {
				var Validator = new IDValidator();
				if (!Validator.isValid(this.userInfo.identity)) {
					uni.$u.toast('身份证格式不正确')
				} else {
					this.$refs.uForm.validate().then(res => {
						let data = this.userInfo;
						console.log(data)
						this.$api.saveUserInfoApi(data).then((res) => {
							if (res.code == 200) {
								let scanRouter = uni.getStorageSync('scanRouter')
								setTimeout(function() {
									if (scanRouter) {
										uni.$u.route('pages/write/index', {
											stationId: uni.getStorageSync('stationId')
										});
									} else {
										uni.$u.route('/pages/user/idCard/idsucc')
									}
								}, 500);
							}
						})
					}).catch(errors => {
						uni.$u.toast('校验失败')
					})
				}
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					let resultArr = JSON.parse(result);
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: resultArr,
						fileName: resultArr.data.ossName,
						fileUrl: resultArr.data.ossUrl,
					}))
					fileListLen++
				}
				this.userInfo.idFront = this.fileList1[0].fileUrl
				this.userInfo.idBack = this.fileList2[0].fileUrl
				this.userInfo.shotImg = this.fileList3[0].fileUrl
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.baseUrl + 'sso/oss/upload', //上传的路径
						filePath: url,
						name: 'file',
						formData: {
							token: uni.getStorageSync('token'),
						},
						header: {
							Authorization: "Bearer " + uni.getStorageSync('token'),
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						}
					});
				})
			},
		}

	}
</script>

<style lang="scss" scoped>
	/deep/ .combox .u-upload__wrap {
		justify-content: center !important;
	}
</style>